package engine.systems.render.render
{
	import ash.tools.ListIteratingSystem;
	
	import engine.classes.enumerations.SystemPriority;
	import engine.utils.DisplayUtils;
	import engine.utils.EntityUtils;
	import engine.utils.MathUtils;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;

	public class RenderSystem extends ListIteratingSystem
	{
		public function RenderSystem()
		{
			super(RenderNode, updateNode, nodeAdded);
			
			this.priority = SystemPriority.RENDER;
		}
		
		public function updateNode(node:RenderNode, time:Number):void
		{
			var display:DisplayObject = node.render._display;
			
			if(EntityUtils.sleeping(node.entity))
			{
				if(!EntityUtils.paused(node.entity))
				{
					display.visible = false;
				}
			}
			else display.visible = node.render.visible;
			
			if(node.render._invalidate)
			{
				node.render._invalidate = false;
				node.render.changed.dispatch(node.entity);
			}
		}
		
		private function nodeAdded(node:RenderNode):void
		{
			if(node.render.display is DisplayObjectContainer)
				DisplayObjectContainer(node.render.display).mouseEnabled = false;
		}
	}
}